package com.fattyCorps.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fattyCorps.an.AEventListener;
import com.fattyCorps.cache.PlayerCache;
import com.fattyCorps.cache.packet.PacketMap;
import com.fattyCorps.config.gameConfig.GlobalMailConfig;
import com.fattyCorps.exception.gameException.mail.MailAlreadyGetAttachmentErrorException;
import com.fattyCorps.exception.gameException.mail.MailNoAttachmentErrorException;
import com.fattyCorps.mapper.SingleMailMapper;
import com.fattyCorps.mgr.event.EEvent;
import com.fattyCorps.obj.cache.PlayerInfo;
import com.fattyCorps.obj.cache.player.packet.Mail;
import com.fattyCorps.obj.mould.GlobalMailMould;
import com.fattyCorps.obj.mould.SingleMailMould;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MailService {

    @Autowired
    PlayerCache playerCache;

    @Autowired
    SingleMailMapper singleMailMapper;

    @Autowired
    ItemService itemService;

    @Autowired
    GlobalMailConfig globalMailConfig;

    @AEventListener(EEvent.EVENT_ON_PLAYER_LOGIN)
    public void onPlayerLogin(PlayerInfo playerInfo) {
        procGlobalMail(playerInfo);
        procSingleMail(playerInfo);
    }

    private void procGlobalMail(PlayerInfo playerInfo) {
        Integer playerId = playerInfo.getId();
        Integer globalMailId = playerInfo.getPlayerData().getGlobalMailId();

        // 如果用户身上的全服邮件id小于当前最新的全服邮件id，需要将所有未发放给用户的全服邮件发给用户
        if (globalMailId < globalMailConfig.getMaxGlobalMailId()) {
            List<GlobalMailMould> disGlobalMailList = globalMailConfig.getDisGlobalMailList(globalMailId);

            // 发放邮件
            for (GlobalMailMould globalMailMould : disGlobalMailList) {
                addMailOnline(playerId, globalMailMould.getTitle(), globalMailMould.getContent(), globalMailMould.getAttachment());
            }

            // 修改用户身上的全服邮件id属性
            playerInfo.getPlayerData().setGlobalMailId(globalMailConfig.getMaxGlobalMailId());
        }
    }

    private void procSingleMail(PlayerInfo playerInfo) {
        QueryWrapper<SingleMailMould> wrapper = new QueryWrapper();
        wrapper.eq("player_id", playerInfo.getId());
        List<SingleMailMould> singleMailMoulds = singleMailMapper.selectList(wrapper);

        for (SingleMailMould singleMailMould : singleMailMoulds) {
            addMailOnline(playerInfo.getId(), singleMailMould.getTitle(), singleMailMould.getContent(), singleMailMould.getAttachment());
        }

        singleMailMapper.delete(wrapper);
    }

    // 获取邮件列表
    public List<Mail> getMailList(Integer playerId) {
        return getPacketMail(playerId).getElems();
    }

    public void addMail(Integer playerId, String title, String content, String attachment) {
        PlayerInfo playerInfo = playerCache.getPlayerInfo(playerId, false);
        if (playerInfo == null) {
            addMailOffline(playerId, title, content, attachment);
        } else {
            addMailOnline(playerId, title, content, attachment);
        }
    }

    // 增加邮件
    public Integer addMailOnline(Integer playerId, String title, String content, String attachment) {
        Mail mail = Mail.makeMail(title, content, attachment);
        getPacketMail(playerId).put(mail);
        return mail.getId();
    }

    public void addMailOffline(Integer playerId, String title, String content, String attachment) {
        SingleMailMould singleMail = new SingleMailMould();
        singleMail.setPlayerId(playerId);
        singleMail.setTitle(title);
        singleMail.setContent(content);
        singleMail.setAttachment(attachment);
        singleMailMapper.insert(singleMail);
    }

    // 读取邮件
    public void readMail(Integer playerId, Integer mailId) {
        Mail mail = getMail(playerId, mailId);
        mail.setRead(true);
    }

    // 领取附件
    public void getAttachment(Integer playerId, Integer mailId) {
        Mail mail = getMail(playerId, mailId);

        // 判断是否有附件
        if (mail.getAttachment().isEmpty()) {
            throw new MailNoAttachmentErrorException();
        }

        // 是否附件已经领取
        if (mail.isGetAttachment()) {
            throw new MailAlreadyGetAttachmentErrorException();
        }

        // 解析邮件附件参数,增加附件道具
        // 附件格式：道具1typeId,道具1数量,道具2typeId,道具2数量。。。
        String[] split = mail.getAttachment().split(",");
        Integer itemTypeId = 0;
        Integer num = 0;

        for (int i = 0; i < split.length; i++) {
            if (i % 2 == 0) {
                // 道具typeId
                itemTypeId = Integer.valueOf(split[i]);
            } else {
                num = Integer.valueOf(split[i]);
                itemService.addItem(playerId, itemTypeId, num);
            }
        }

        // 设置附件为已领取
        mail.setGetAttachment(true);
    }

    public PacketMap<Mail> getPacketMail(Integer playerId) {
        return playerCache.getPlayerInfo(playerId).getPlayerData().getMails();
    }

    public Mail getMail(Integer playerId, Integer id) {
        return playerCache.getPlayerInfo(playerId).getPlayerData().getMails().get(id);
    }
}
